package com.lzm.ydpt.module.order.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.amap.api.maps2d.model.LatLng;
import com.amap.api.services.core.PoiItem;
import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonutils.rx.RxBus;
import com.byt.framlib.commonwidget.NormalTitleBar;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.Dapplication;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.AddressBook;
import com.lzm.ydpt.entity.AddressBookBus;
import com.lzm.ydpt.module.common.MapAddressActivity;
import com.lzm.ydpt.mvp.contract.AddAddressBookContract;
import com.lzm.ydpt.mvp.presenterimpl.AddAddressBookPresenterImpl;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 添加地址簿
 */
public class AddAddressBookActivity extends BaseActivity<AddAddressBookPresenterImpl> implements AddAddressBookContract.View {
    private static final int ADDRESS_CODE = 0x01;

    @BindView(R.id.ntb_apend_address)
    NormalTitleBar ntb_apend_address;

    @BindView(R.id.img_fill_address_type)
    ImageView img_fill_address_type;
    @BindView(R.id.tv_fill_address_type)
    TextView tv_fill_address_type;

    @BindView(R.id.ed_address_username)
    EditText ed_address_username;
    @BindView(R.id.ed_address_phone)
    EditText ed_address_phone;
    @BindView(R.id.tv_address_region)
    EditText tv_address_region;
    @BindView(R.id.tv_select_region)
    TextView tv_select_region;
    @BindView(R.id.ed_detailed_address)
    EditText ed_detailed_address;

    /**
     * 类型
     * 1 发货地址
     * 2 送货地址
     */
    private int type = 1;
    private AddressBook mAddressBook = null;
    private PoiItem mPoiItem = null;
    private LatLng mLatLng = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_address_book;
    }

    @Override
    public AddAddressBookPresenterImpl initPreData() {
        return new AddAddressBookPresenterImpl(this);
    }

    @Override
    public void initView() {
        setNtbTitle(ntb_apend_address, false);
        type = getIntent().getIntExtra(Constants.INP_APPEND_ADDRESS_TYPE, 1);
        mAddressBook = getIntent().getParcelableExtra(Constants.INP_APPEND_ADDRESS_BEAN);
        initTitle();
        if (type == 1) {
            img_fill_address_type.setImageResource(R.drawable.ic_mail);
            tv_fill_address_type.setText("发件人");
        } else {
            img_fill_address_type.setImageResource(R.drawable.ic_delivery);
            tv_fill_address_type.setText("收件人");
        }
        initData();
    }

    private void initTitle() {
        ntb_apend_address.setTitleText("新增地址");
        ntb_apend_address.setOnBackListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        if (mAddressBook != null) {
            ed_address_username.setText(mAddressBook.getContacts());
            ed_address_phone.setText(mAddressBook.getPhone());
            tv_address_region.setText(mAddressBook.getAddress());
            ed_detailed_address.setText(mAddressBook.getDetailedAddress());
            mLatLng = new LatLng(mAddressBook.getLatitude(), mAddressBook.getLongitude());
        }
    }

    @OnClick({R.id.ll_select_region, R.id.add_save_addess})
    public void OnClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.ll_select_region:
                Bundle addressBundle = new Bundle();
                addressBundle.putParcelable(MapAddressActivity.INP_POIITEMS_INFO, mLatLng);
                startActivityForResult(MapAddressActivity.class, addressBundle, ADDRESS_CODE);
                break;
            case R.id.add_save_addess:
                if (isCheck()) {
                    addAddressBook();
                }
                break;
        }
    }

    private boolean isCheck() {
        if (TextUtils.isEmpty(ed_address_username.getText().toString())) {
            showShortToast("请输入联系人");
            return false;
        }
        if (TextUtils.isEmpty(ed_address_phone.getText().toString())) {
            showShortToast("请输入手机号");
            return false;
        }
        if (TextUtils.isEmpty(tv_address_region.getText().toString())) {
            showShortToast("请选择地址");
            return false;
        }
        if (TextUtils.isEmpty(ed_detailed_address.getText().toString())) {
            showShortToast("请输入门牌号");
            return false;
        }
        return true;
    }

    private void addAddressBook() {
        FormBodys.Builder formBodys = new FormBodys.Builder();
        formBodys.add("memberId", Dapplication.getUserInfo().getMemberId());
        formBodys.add("type", type);
        formBodys.add("contacts", ed_address_username.getText().toString());
        formBodys.add("phone", ed_address_phone.getText().toString());
        formBodys.add("address", tv_address_region.getText().toString());
        formBodys.add("detailedAddress", ed_detailed_address.getText().toString());

        if (mLatLng == null) {
            if (mAddressBook != null) {
                formBodys.add("id", mAddressBook.getId());
                formBodys.add("longitude", mAddressBook.getLongitude());
                formBodys.add("latitude", mAddressBook.getLatitude());
            }
        } else {
            formBodys.add("longitude", mLatLng.longitude);
            formBodys.add("latitude", mLatLng.latitude);
        }
        startProgressDialog();
        mPresenter.onAddAddressBook(formBodys.build());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ADDRESS_CODE) {
                PoiItem mPoiItem = data.getParcelableExtra(MapAddressActivity.INP_POIITEMS_INFO);
                if (mPoiItem != null) {
                    this.mPoiItem = mPoiItem;
                    mLatLng = new LatLng(mPoiItem.getLatLonPoint().getLatitude(), mPoiItem.getLatLonPoint().getLongitude());
                }
                tv_address_region.setText(mPoiItem.getProvinceName() + mPoiItem.getCityName() + mPoiItem.getAdName());
                ed_detailed_address.setText(mPoiItem.getTitle());
                ed_detailed_address.setSelection(ed_detailed_address.length());
                tv_select_region.setText(mPoiItem.getCityName());
            }
        }
    }

    @Override
    public void onAddAddressBook(String msg) {
        stopProgressDialog();
        showShortToast(msg);
        RxBus.getDefault().postSticky(new AddressBookBus());
        finish();
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {

    }
}